#pragma once

#include <iostream>
#include <sys/epoll.h>
#include "log.hpp"
using namespace logmodule;
class epoller
{
    public:
    epoller():_epfd(-1)
    {
        _epfd = epoll_create(128);
        if(_epfd<0)
        {
            //打印错误日志
            LOG(loglevel::FATAL)<<"epollcreate error!!!";
        }
        LOG(loglevel::DEBUG)<<"create epoll success"<<_epfd;
    }
    void AddEvent(int sockfd,uint32_t events)
    {
        struct epoll_event ev;
        ev.events = events;
        ev.data.fd = sockfd;
        int n = epoll_ctl(_epfd,EPOLL_CTL_ADD,sockfd,&ev);
        if(n<0)
        {
            LOG(loglevel::ERROR)<<"epoll_ctl error";
            return ;
        }
        LOG(loglevel::INFO)<<"epoll_ctl success ";
    }
    int Waitevents(struct epoll_event revs[],int maxnum,int timeout)
    {
        int n = epoll_wait(_epfd,revs,maxnum,timeout);
        if(n<0)
        {
            LOG(loglevel::FATAL)<<"epoll wait error";
        }
        return n;
    }
    ~epoller()
    {
        if(_epfd>=0)
        {
            close(_epfd);
        }
    }
    private:
    int _epfd;
};